package com.yupi.mianshiya.controller;

import com.yupi.mianshiya.common.BaseResponse;
import com.yupi.mianshiya.common.ErrorCode;
import com.yupi.mianshiya.common.ResultUtils;
import com.yupi.mianshiya.exception.BusinessException;
import com.yupi.mianshiya.exception.ThrowUtils;
import com.yupi.mianshiya.model.entity.Articles;
import com.yupi.mianshiya.model.entity.User;
import com.yupi.mianshiya.service.ArticlesService;
import com.yupi.mianshiya.service.LikeService;
import com.yupi.mianshiya.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@Api(tags = "点赞接口")
@RestController
@RequestMapping("/like")
public class LikeController {
    @Resource
    private LikeService likeService;

    @Resource
    private UserService userService;

    @Resource
    private ArticlesService articlesService;

    @PostMapping("/toggle")
    @ApiOperation("用户点赞或取消点赞")
    public BaseResponse<Long> toggleLike(

            @RequestParam Long articleId, HttpServletRequest request) {

        // 参数校验
        if (articleId == null || articleId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文章ID无效");
        }

        User loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        //查询文章是否存在
        Articles articles = articlesService.getById(articleId);
        ThrowUtils.throwIf(articles == null, ErrorCode.NOT_FOUND_ERROR, "文章不存在");

        Long userId = loginUser.getId();
        try {
            Long likeCount = likeService.toggleLike(userId, articleId);
            return ResultUtils.success(likeCount);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "点赞操作失败");
        }
    }

    /**
     * 获取点赞状态
     *
     *
     * @param articleId 文章ID
     * @return 是否已点赞
     */
    @ApiOperation("获取当前用户点赞状态")
    @GetMapping("/status")
    public BaseResponse<Boolean> getLikeStatus(@RequestParam Long articleId, HttpServletRequest request) {
        // 参数校验

        if (articleId == null || articleId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文章ID无效");
        }
        //查询文章是否存在
        Articles articles = articlesService.getById(articleId);
        ThrowUtils.throwIf(articles == null, ErrorCode.NOT_FOUND_ERROR, "文章不存在");

        User loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);
        Long userId = loginUser.getId();
        try {
            Boolean hasLiked = likeService.getLikeStatus(userId, articleId);
            return ResultUtils.success(hasLiked);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "获取点赞状态失败");
        }
    }

    /**
     * 获取文章点赞数
     *
     * @param articleId 文章ID
     * @return 点赞数
     */
    @ApiOperation("获取文章点赞数")
    @GetMapping("/count")
    public BaseResponse<Long> getLikeCount(
            @RequestParam Long articleId, HttpServletRequest request) {

        // 参数校验
        if (articleId == null || articleId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "文章ID无效");
        }
        User loginUser = userService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null, ErrorCode.NOT_LOGIN_ERROR);

        //查询文章是否存在
        Articles articles = articlesService.getById(articleId);
        ThrowUtils.throwIf(articles == null, ErrorCode.NOT_FOUND_ERROR, "文章不存在");

        try {
            Long likeCount = likeService.getLikeCount(articleId);
            return ResultUtils.success(likeCount);
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "获取点赞数失败");
        }
    }

}
